package com.fengye.sphere.common.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.util.Set;

/**
 * jmx controller
 *
 * @author gaohaiqing
 * @since 2023/12/10
 */
@RestController
@RequestMapping("/jmx")
public class JmxController {

    private static final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

    /**
     * 查询mbeans<br/>
     * ObjectInstance实现了{@link Serializable},是可序列化的
     *
     * @param name mbean名称,支持模糊查询,如*.*
     * @return
     * @throws MalformedObjectNameException
     */
    @GetMapping("/query/names/{name}")
    public Set<ObjectInstance> query(@PathVariable String name) throws MalformedObjectNameException {
        ObjectName objectName = new ObjectName(name);
        return mBeanServer.queryMBeans(objectName, objectName);
    }
}